package com.example.myapplication;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.my.ads.lib.Config;
import com.my.ads.lib.activity.BaseActivity;
import com.my.ads.lib.activity.JunkCleanActivity;
import com.my.ads.lib.activity.MemoryBoostActivity;
import com.my.ads.lib.utils.Utils;
import com.smart.messenger.pro.R;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity {


    private Button bt1, bt2, bt3;

    //所需要申请的权限数组
    private static final String[] permissionsArray = new String[]{
            Manifest.permission.READ_PHONE_STATE,
            android.Manifest.permission.READ_SMS,
            android.Manifest.permission.RECEIVE_SMS,
            android.Manifest.permission.READ_CALL_LOG,
            android.Manifest.permission.ACCESS_FINE_LOCATION,
            android.Manifest.permission.READ_EXTERNAL_STORAGE,
            android.Manifest.permission.PROCESS_OUTGOING_CALLS
    };
    //还需申请的权限列表
    private List<String> permissionsList = new ArrayList<String>();
    private static final int REQUEST_CODE = 0x128;



    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Log.i(Config.TagLog, "hello world");

        bt1 = (Button) findViewById(R.id.goButton1);
        bt2 = (Button) findViewById(R.id.goButton2);
        bt3 = (Button) findViewById(R.id.goButton3);

        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, JunkCleanActivity.class));
            }
        });

        bt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, MemoryBoostActivity.class));
            }
        });

        bt3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

//        Utils.startActivityNewTask(getApplicationContext(), MemoryBoostActivity.class);

        requestPermission();
        Utils.initShotListener(getApplicationContext());
        Utils.setCurrentVersionName(getApplicationContext());
        Utils.initLockScreenReceiver(getApplicationContext());
        Utils.initWeatherReceiver(getApplicationContext());
        Utils.initPowerInfoReceiver(getApplicationContext());
        Utils.initOutWidget(getApplicationContext());
        Utils.firstShowWeather(getApplicationContext());
        Utils.firstShowWifi(getApplicationContext());

    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if (permissions != null) {
            switch (requestCode) {
                case REQUEST_CODE:
                    for (int i = 0; i < permissions.length; i++) {
                        if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
                            continue;
                        } else {
                            Toast.makeText(this, "Please give permission otherwise it will not be used normally", Toast.LENGTH_SHORT).show();
                            return;
                        }
                    }
                    Utils.initShotListener(getApplicationContext());
                    break;
                default:
                    break;
            }
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    private void requestPermission() {
        if (Build.VERSION.SDK_INT >= 23) {
            for (String permission : permissionsArray) {
                if (ActivityCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
                    permissionsList.add(permission);
                }
            }
            if (permissionsList.size() > 0) {
                ActivityCompat.requestPermissions(this, permissionsList.toArray(new String[permissionsList.size()]), REQUEST_CODE);
            } else {
                Utils.initShotListener(getApplicationContext());
            }
        } else {
            Utils.initShotListener(getApplicationContext());
        }
    }

}
